var form = {
    isoperate: false,
    go: function (url) {
        window.location = url;
    },
    ajax: function (url, data, callback) {
        if (form.isoperate) {
            Page.$message({
                message: '正在操作中...',
                type: 'warning'
            });
            return;
        }
        form.isoperate = true;
        miiteen.ajax.post(url, data, callback, form.errorcallback);
    },
    successcallback: function (response) {
        var result = response.data;
        if (result.state > 0) {
            window.parent.mtMsg.success(result.message);
            if (result.code == "redirect") {
                //跳转页面
                form.go(result.data);
            }
        } else {
            window.parent.mtMsg.error(result.message);
        }
        form.isoperate = false;
    },
    errorcallback: function () {
        form.isoperate = false;
    }
}

/**
 * 列表界面
 * @type {{init: form.list.init, new: form.list.new, del: form.list.del, search: form.list.search, method: {getgridheight: form.list.method.getgridheight}}}
 */
form.list = {
    init: function (page) {
        page.grid.height = this.method.getgridheight();
    },
    add: function (page) {
        form.go(page.controller + "/edit");
    },
    del: function (page, param) {
        if (page.gridselect.length < 1) {
            window.parent.mtMsg.warnbox("请选择要删除的数据");
            return;
        }
        var ids = "";
        for (var i = 0; i < page.gridselect.length; i++) {
            ids += (ids == "" ? "" : ",") + page.gridselect[i].id;
        }

        miiteen.ajax.postconfirm(page.controller + "/del?ids=" + ids, param ? param : {}, "确认要删除选择的数据吗？", function (response) {
            form.successcallback(response);
            //删除后刷新
            form.list.search(page, page.controller + "/search");
        }, form.errorcallback)
    },
    edit: function (page, id) {
        form.go(page.controller + "/edit?id=" + id);
    },
    view: function (page, id) {
        form.go(page.controller + "/view?id=" + id);
    },
    search: function (page, url) {
        var con = {searchcon: page.form, pagecon: {pageSize: page.grid.pageSize, pageNum: page.grid.curPage}};
        miiteen.ajax.post(url, con, function (response) {
            var result = response.data;
            if (result.state > 0) {
                page.grid.data = result.data;
                page.grid.total = Number(result.message);
            } else {
                page.grid.data = [];
            }
        }, function () {

        })
    },
    method: {
        getgridheight: function () {
            var mainheight = document.getElementsByClassName("mainpage")[0].clientHeight;
            var topmenus = document.getElementsByClassName("page-topmenu");
            var forms = document.getElementsByClassName("el-form");

            var gridheight = mainheight;
            for (var i = 0; i < topmenus.length; i++) {
                gridheight -= topmenus[i].clientHeight;
            }
            for (var i = 0; i < forms.length; i++) {
                gridheight -= forms[i].clientHeight;
            }
            gridheight -= (30 + 21 + 38);
            if (gridheight < 300) {
                return 300;
            } else {
                return gridheight;
            }
        }
    }
}

//gird相关
form.grid = {
    height: 300,
    data: [],
    total: 0,
    pagelayout: "sizes, prev, pager, next",
    pageSizes: [10, 20, 50, 100],
    pageSize: 10,
    curPage: 1
}

/**
 * 编辑页面
 * @type {{back: form.edit.back, save: form.edit.save}}
 */
form.edit = {
    labelwidth: "100px",
    labelposition: "right",
    back: function (page) {
        //返回
        window.history.back();
    },
    save: function (page, formName, formdata, url, callback) {
        //保存
        page.$refs[formName].validate(
            function (valid) {
                if (valid) {
                    form.ajax(url, formdata, callback ? callback : form.successcallback);
                } else {
                    return false;
                }
            }
        );
    }
}

/**
 * 查看页面
 * @type {{}}
 */
form.view = {
    labelwidth: "60px",
    labelposition: "right",
    back: function (page) {
        //返回
        window.history.back();
    }
}

form.method = {}